def min_cost_assignment(cost_matrix):
    n = len(cost_matrix)
    dp = [[float('inf')] * n for _ in range(n)]
    for j in range(n):
        dp[0][j] = cost_matrix[0][j]
    for i in range(1, n):
        for j in range(n):
            for k in range(n):
                if k != j:
                    dp[i][j] = min(dp[i][j], dp[i-1][k] + cost_matrix[i][j])
    min_cost = min(dp[n-1])
    return min_cost

with open('input.txt', 'r') as file:
    n = int(file.readline().strip())
    cost_matrix = []
    for _ in range(n):
        line = list(map(int, file.readline().strip().split()))
        cost_matrix.append(line)

min_cost = min_cost_assignment(cost_matrix)

with open('output.txt', 'w') as file:
    file.write(str(min_cost))
